import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies from 'js-cookie'

Vue.use(VueRouter);

const router = new VueRouter({
    mode:'history',
    base: process.env.BASE_URL,
    routes: [
        {
            path: '/',
            name: 'home',
            component: () => import('./views/careerPlan.vue'),
            meta: {keep_alive:true,name:'首页'},
            redirect:'/plan',  // 默认路由
            children: [
                /*******************************一级路由**************************************/
                {
                    path: '/plan',
                    name: 'plan',
                    component: () => import('./views/careerPlan/plan.vue'),
                    meta: {keep_alive:true,name:'生涯规划'}    // keep_alive   是否需要登录
                },
                {
                    path: '/class',
                    name: 'class',
                    component: () => import('./views/careerClass/class.vue'),
                    meta: {keep_alive:true,name:'生涯课堂'}
                },
                {
                    path: '/bank',
                    name: 'bank',
                    component: () => import('./views/careerBank/bank.vue'),
                    meta: {keep_alive:true,name:'生涯宝库'}
                },
                {
                    path: '/appraisal',
                    name: 'appraisal',
                    component: () => import('./views/careerAppraisal/appraisal.vue'),
                    meta: {keep_alive:true,name:'生涯测评'}
                },
                {
                    path: '/select-sub',
                    name: 'selectSub',
                    component: () => import('./views/intellectSelectSub/selectSub.vue'),
                    meta: {keep_alive:true,name:'6选3智能选科'}
                },
                {
                    path: '/apply',
                    name: 'apply',
                    component: () => import('./views/volunteerApply/apply.vue'),
                    meta: {keep_alive:true,name:'志愿填报'}
                },
                {
                    path: '/my-career',
                    name: 'myCareer',
                    component: () => import('./views/myCareer/myCareer.vue'),
                    meta: {keep_alive:true,name:'我的生涯'}
                },
                /*******************************二级路由**************************************/
                {
                    path: '/bank/child-bank',
                    name: 'childBank',
                    component: () => import('./views/careerBank/children/childBank.vue'),
                    meta: {keep_alive:true,name:'信息库'}  // 专业信息库、职业信息库、院校信息库
                },
                {
                    path: '/bank/major-details',
                    name: 'majorDetails',
                    component: () => import('./views/careerBank/majorDetails/majorDetail.vue'),
                    meta: {keep_alive:true,name:'专业详情'},
                },
                /*******************************其他路由**************************************/
                {
                    path: '/empty',
                    name: 'empty',
                    component: () => import('./views/common/empty/empty.vue'),
                    meta: {keep_alive:false,name:'空状态'}
                },
                {
                    path: '/limits-auth',
                    name: 'limitsAuth',
                    component: () => import('./views/common/limitsAuth/limitsAuth.vue'),
                    meta: {keep_alive:false,name:'权限不足'}
                },
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('./views/login/login.vue'),
            meta: {keep_alive:false,name:'NEWEWT登录'}
        },
        {
            path: '/user-center',
            name: 'userCenter',
            component: () => import('./views/userCenter/userCenter.vue'),
            meta: {keep_alive:true,name:'用户中心'}
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('./views/login/register/register.vue'),
            meta: {keep_alive:false,name:'新用户注册'}
        },
        {
            path: '/forget',
            name: 'forget',
            component: () => import('./views/login/forget/index.vue'),
            meta: {keep_alive:false,name:'找回账号/密码'},
            redirect: '/forget/index',  // 默认路由
            children: [
                {
                    path: '/forget/index',
                    name: 'forgetPwd',
                    component: () => import('./views/login/forget/forgetPwd.vue'),
                    meta: {keep_alive:false,name:'找回账号'},
                },
                {
                    path: '/forget/account',
                    name: 'forgetAccount',
                    component: () => import('./views/login/forget/account/account.vue'),
                    meta: {keep_alive:false,name:'找回账号'},
                },
                {
                    path: '/forget/password',
                    name: 'forgetPassword',
                    component: () => import('./views/login/forget/password/password.vue'),
                    meta: {keep_alive:false,name:'找回密码'},
                }
            ]
        },
        {
            path: '/service-center',
            name: 'serviceCenter',
            component: () => import('./views/serviceCenter/serviceCenter.vue'),
            meta: {keep_alive:false,name:'服务中心'}
        },
    ]
});

router.beforeEach((to, from, next) => {
    // let token = Cookies.get('token');
    let token = JSON.parse(sessionStorage.getItem('token'))
    // console.log(from);
    // console.log(token);
    // 判断页面是否需要登录
    if(to.meta.keep_alive) {
        // 判断用户是否登录
        if(token) {
            document.title = to.meta.name;
            next();
        }else{
            next({
                path: '/login',
                query: {
                    sId: 1,
                    fromUrl: '',
                }
            })
        }
    }else{
        document.title = to.meta.name;
        next();
    }
});

export default router